#include "serial.h"
#include "wifi.h"
#include "OLED.h"
#include "NTP.h"
#include "weather.h"

void showAllInfo()
{
  OLED_ShowString(0, 0, ntp_GetDate(), 16);
  OLED_ShowChinese(90, 0, 0, 16);
  OLED_ShowChinese(107, 0, 1, 16);
  OLED_ShowString(0, 2, ntp_GetTime(), 16);
  get_weatherInfo();
}


void NetworkServer()
{
  if(wifi_Connect(WIFI_SSID, WIFI_PASSWORD))
  {
    OLED_Clear();
    OLED_ShowString(0, 0, "WIFI Successful", 8); 
    delay(1000);
  }
  else
  {
    OLED_Clear();
    OLED_ShowString(0, 0, "WIFI Failed", 8);  
  }

  OLED_Clear();
  OLED_ShowString(0, 0, "NTP Connecting", 8);

  if(ntpSync())
  {
    OLED_Clear();
    OLED_ShowString(0, 0, "NTP Successful", 8); 

    delay(1000);
  }
  else
  {
    OLED_Clear();
    OLED_ShowString(0, 0, "NTP Failed", 8);   
  }

  OLED_Clear();
  OLED_ShowString(0, 0, "Weather Connecting", 8); 

  if(weather_update())
  {
    OLED_Clear();
    OLED_ShowString(0, 0, "Weather Successful", 8);
    delay(1000); 
  }
  else
  {
    OLED_Clear();
    OLED_ShowString(0, 0, "Weather Failed", 8);   
  }
}

void setup() 
{
  // put your setup code here, to run once:
  serial_Init(9600);
  
  OLED_Init();
  OLED_ShowString(0, 0, "System Booting", 16);
  
  wifi_Init();
  
  ntp_Init();

  weather_Init();

  NetworkServer();
  OLED_Clear();
}

void loop() 
{
  // put your main code here, to run repeatedly:
  showAllInfo();

  static uint32_t tmp = 0;
  uint32_t current_tmp = millis();

  if(current_tmp - tmp >= 30000)
  {
    Serial.print(0x71);
    Serial.print(hour);
    Serial.print(minute);
    Serial.print(0x81); 
    tmp = current_tmp;
  }
  
}
